//
//  MainViewController.swift
//  Weibo1.0
//
//  Created by lianghuahao on 15/12/9.
//  Copyright © 2015年 lianghuahao.com. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    //MARK: - 添加点击方法
    @objc private func clickComposedButton() {
    
        print("点我了")
    
    }
    //MARK: - 视图生命周期方法
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        //添加控制器并不会创建tabbar 中的按钮
        //懒加载无处不在的, 所有控件都是延迟创建的
        addChildViewControllers()
        
        print(tabBar.subviews)
        
        //函数的说明要用三个斜线,在调用函数的时候也会显示该函数的说明(如下函数)
        setComposedButton()
    }
    
    override func viewWillAppear(animated: Bool) {
        //会创建tabbar 中的所有控制器对应的按钮
        super.viewWillAppear(animated)
        
        //将按钮弄到最前面
        tabBar.bringSubviewToFront(composedButton)
    }
    //MARK: - 懒加载控件
    private lazy var composedButton: UIButton = UIButton(
        imageName: "tabbar_compose_icon_add",
        backImageName: "tabbar_compose_button")
/*
    {
         自定义按钮
    
        let button = UIButton()
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.sizeToFit()
    
        return button
    }()
*/
}
//MARK: - 设置界面
//extension 类似于OC的分类,分类中不能定义"存储性"属性!!  Swift中同样
extension MainViewController {
    
   //函数的说明要用三个斜线,在调用函数的时候也会显示该函数的说明(如下函数)
    ///设置撰写按钮
    private func setComposedButton() {
        //1添加按钮
        tabBar.addSubview(composedButton)
        //2调整按钮位置
        let count = childViewControllers.count
        let w = tabBar.bounds.width / CGFloat(count) - 1
        
        composedButton.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
        
        composedButton.addTarget(self, action: "clickComposedButton", forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    ///添加所有控制器
    
    private func addChildViewControllers() {
        
        //设置tintcolor - 图片渲染颜色
        //能用颜色解决,就不用图片
//        tabBar.tintColor = UIColor.orangeColor()
        
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
    }
 
    /// 添加控制器
    ///
    /// - parameter vc:        vc
    /// - parameter title:     标题
    /// - parameter imageName: 图片名称
    private func addChildViewController(vc : UIViewController, title : String, imageName : String) {
        
//        let vc = HomeTableViewController()
        
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: imageName)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }

}
